<?php
/*! This class plots weekly Graph for all groups | Also contains Group Aggregation function */
class Graph
{
//! Function to plot weekly graph of all groups
	function weekly_graph()
	{
		// Initialize chart parameters
		
		$color =  array("FF0000","00FF00","0000FF",
						"000000","00a040","00ffff",
						"800000","40e0d0","ff00ff",
						"ffff00","a06040","4060ff",
						"ffffc0","F0E68C","FF8C00");
		
		$max_post=0; //This is the max num of posts for all group, used to specify Y-axis range in graph
		
		$time=$this->get_min_max_time();
				
		# echo strtotime('+7 days', $row1[0]);
		
		for($j=1;$j<=$time['num_groups'];$j++) //Iterate for all groups
		{

			//Save group names in an array
			$group_name_query = mysql_query("SELECT `name` from `group` where `id`=".$j) or die(mysql_error());
			$row_group_name = mysql_fetch_row($group_name_query);
			$group[$j]= $row_group_name[0];

			//Count all forum posts
			$result1 =  mysql_query("SELECT count(*) FROM `forum` WHERE `group_id`=".$j) or die(mysql_error());
			$row1 = mysql_fetch_row($result1);
			printf($row1[0]."<br>");
			
			
			// Store weekly posts in an array
			
			$weekly_posts = array();
			$i=1;	//Means week 1
			
			//Get start/end dates 
			$start_date=$time['min'];
			$end_date=$time['max'];
			
			
			while($start_date<$end_date)
			{
				
				printf("Week".$i."<br>");
				
				$post_tables = array('forum','file','issue','svn','wiki');
				$id_group = mysql_query("SELECT `id`,`name` from `group` ") or die(mysql_error());
				
				
				$count=0;
				foreach($post_tables as $table)
				{
					$count_posts = mysql_query("SELECT COUNT(*) FROM $table WHERE `group_id`=".$j." and date between ".$start_date." and ".strtotime('+7 days', $start_date)) or die(mysql_error());
					$c=mysql_fetch_row($count_posts);
					$count+=$c[0];
				}
				$weekly_posts[$i++]=$count; // $id[1] is the full_name
				$corr['svn']= 
				$start_date = strtotime('+7 days', $start_date);
				
			}
				
			
			//Save max_posts to specify Y-axis range
			if(max($weekly_posts)>$max_post)
			$max_post=max($weekly_posts);
			
			
			//Creating query parameter 'chd'
			$chd = "&chd=t:";
			$chxl = "&chxl=0:"; //0: means plot for x axis
			
			print_r($weekly_posts);
			
			$length = count($weekly_posts);
			for ($i = 1; $i < $length ; $i++) {
			$chd = $chd.$weekly_posts[$i].",";
			$chxl = $chxl."|"."Week".$i; //Stores week 1...n
			}
			
			//Readjusting last argument
			$chd = $chd.$weekly_posts[$length]; 
			$chxl = $chxl."|"."Week".$i;
			
			echo '<br>This is chd argument:  '.$chd;
			echo '<br>This is chxl argument:  '.$chxl;
			
			$x_axis[$j]=$chd; //Storing x-axis in array
				
		}
		
		$chxl.="|3:|No. of posts"; //Add number of posts label
		
		// Attach multiple graph lines 
		for($i=1;$i<=count($x_axis);$i++)
		{
			if($i>1)
			{
				$chd =$chd."|".substr($x_axis[$i],7);
			}
			else
			{
				$chd = $x_axis[$i];
			}
		}
	
		$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y".$chd.$chxl;

		//Add colors to lines 
		$chco = "&chco=";
		if(count($x_axis)>1)
		for($i=0;$i<count($x_axis);$i++) //can start from 0 since only color parameter is changed
			$chco = $chco.$color[$i].",";
			$chco = substr($chco,0,-1); //handle last comma
		
		$graph.=$chco;
		
		//Add group labels
		$chdl ="&chdl=";
		if(count($x_axis)>1)
		{
		for($i=1;$i<=count($x_axis);$i++) //can start from 0 since only color parameter is changed
			$chdl = $chdl.$group[$i]."|";
			$chdl = substr($chdl,0,-1); //handle last comma		
		}
		
		$graph.=$chdl;
		
		//Add line thickness
		
		$chls = "&chls=";
		$line_thickness=3;
		if(count($x_axis)>1)
		{
		for($i=1;$i<=count($x_axis);$i++) //can start from 0 since only color parameter is changed
			$chls = $chls.$line_thickness."|";
			$chls = substr($chls,0,-1); //handle last comma		
		}
			
		echo $chls;
		
		$graph.=$chls;
		
		//Add background
		
		$backgrnd = "&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25";
		
		$graph.=$backgrnd;
		
		
		//Specify Y-Axis range
		//Adjust max post range to nearest multiple of 10
		while($max_post%10!=0)
		$max_post++;
		$max_post+=10; //plot 10 extra points for clarity
		
		$chxr="&chxr=1,0,".$max_post.",10"."|2,0,".$max_post.",10"."&chds=0,".$max_post; //$max_post - Find max number of posts of a group and specify range accordingly: 1-1st Y axis of (x,y,r,y) and 2-right Y-axis of (x,y,r,y)
		$graph.=$chxr;
		
		
		//Specify Y-axis label position
		$chxp="&chxp=3,50";
		
		$graph.=$chxp;
		
		//Chart title-- not implemented yet
		//$result_course =  mysql_query("SELECT title FROM `course`,`instructor` WHERE `instructor`.`full_name`=$_SESSION['logged_in'] AND group_id`=".$j." and date between ".$start_date." and ".strtotime('+7 days', $start_date)) or die(mysql_error());
		//$row_course = mysql_fetch_row($result_per_week_wiki);
		//$course = $row_course[0];
					
		$chtt = "&chtt=Weekly%20Chart%20&chts=000000,16";
		$graph.=$chtt;
		
		#echo $graph;	
		
		//This are sample $graph actually generated
		#$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y&chd=t:4,4,4,18,75,21,5|5,4,4,19,30,11,1|10,10,8,30,30,12,4&chxl=0:|Week1|Week2|Week3|Week4|Week5|Week6|Week7|3:|No.%20of%20posts&chco=FF0000,00FF00,0000FF&chdl=etat|etat1|etat2&chls=3|3|3&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chxp=3,50";
		#$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y&chd=t:6,10,40,15,31|6,3,7,2,6|0,10,33,15,16&chxl=0:|Week1|Week2|Week3|Week4|Week5|3:|No.%20of%20posts&chco=FF0000,00FF00,0000FF&chdl=Group1|Group2|Group3&chls=3|3|3&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chxp=3,50";
		#$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y&chd=t:6,10,40,15,31|6,3,7,2,6|0,10,33,15,16&chxl=0:|Week1|Week2|Week3|Week4|Week5|3:|No.%20of%20posts&chco=FF0000,00FF00,0000FF&chdl=Group1|Group2|Group3&chls=3|3|3&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chxr=1,0,50,10|2,0,50,10&chds=0,50&chxp=3,50&chtt=Weekly%20Chart%20(Course%201)&chts=000000,16";
		echo "<meta http-equiv=\"Refresh\" content=\"0; url=".$graph."\">";
		
		// echo date('n/j/Y',"1266028971"); //check to confirm date
	
	}
	
	function aggregate_group()
	{
		$post_tables = array('forum','file','issue','svn','wiki');
		$id_group = mysql_query("SELECT `id`,`name` from `group` ") or die(mysql_error());
		
		while($id=mysql_fetch_row($id_group))
		{
			$count=0;
			foreach($post_tables as $table)
			{
				$count_posts = mysql_query("SELECT count(*) from $table where `group_id`=$id[0] ") or die(mysql_error());
				$c=mysql_fetch_row($count_posts);
				$count+=$c[0];
			}
			$aggr[$id[1]]=$count; // $id[1] is the full_name
		}
		
		arsort($aggr); //Sort based on value
 		
		echo "Aggregate groups<br>";
		foreach($aggr as $key=>$value)
			echo $key."=".$value."<br>";
		
			//Add logic to print aggregate
	}
	
	function aggregate_student()
	{
		$post_tables = array('forum','file','issue','svn','wiki');
		$id_student = mysql_query("SELECT `id`,`full_name` from `student` ") or die(mysql_error());
		
		while($id=mysql_fetch_row($id_student))
		{
			$count=0;
			foreach($post_tables as $table)
			{
				$count_posts = mysql_query("SELECT count(*) from $table where `author`=$id[0] ") or die(mysql_error());
				$c=mysql_fetch_row($count_posts);
				$count+=$c[0];
			}
			$aggr[$id[1]]=$count; // $id[1] is the full_name
		}
		
		arsort($aggr); //Sort based on value
 		
		echo "Aggregate students<br>";
		foreach($aggr as $key=>$value)
			echo $key."=".$value."<br>";
		
		//Add logic to print aggregate
		
	}
	
	// Get correlation between SVN and Forum, Wiki count and File 
	function correlation()
	{
	
		$color =  array("FF0000","00FF00","0000FF",
						"000000","00a040","00ffff",
						"800000","40e0d0","ff00ff",
						"ffff00","a06040","4060ff",
						"ffffc0","F0E68C","FF8C00");
		
		
		$max_post=0; //This is the max num of posts for all group, used to specify Y-axis range in graph
		
		$time=$this->get_min_max_time();
		
		$start_date=$time['min'];
		$end_date=$time['max'];
		$i=1; //First week
		
		
		while($start_date<$end_date)
		{
			$count_posts = mysql_query("SELECT COUNT(*) FROM `forum` WHERE `date` between ".$start_date." and ".strtotime('+7 days', $start_date)) or die(mysql_error());
			$c=mysql_fetch_row($count_posts);
			$weekly_forum[$i]=$c[0];
			
			$count_posts = mysql_query("SELECT COUNT(*) FROM `svn` WHERE `date` between ".$start_date." and ".strtotime('+7 days', $start_date)) or die(mysql_error());
			$c=mysql_fetch_row($count_posts);
			$weekly_svn[$i++]=$c[0];
			$start_date = strtotime('+7 days', $start_date);
		}
		print_r($weekly_forum);
		print_r($weekly_svn);
			
		
		//Save max_posts to specify Y-axis range
		$y_range = array(max($weekly_forum),max($weekly_svn),$max_post) or die(mysql_error());
		$max_post=max($y_range);
		
		//Creating query parameter 'chd' (Graph points)
		$chd = "&chd=t:";
		$chxl = "&chxl=0:"; //0: means plot for x axis
		
		
		$length = count($weekly_forum);
		for ($i = 1; $i < $length ; $i++) {
		$chd = $chd.$weekly_forum[$i].",";
		$chxl = $chxl."|"."Week".$i; //Stores week 1...n
		}
		
		$chd = $chd.$weekly_forum[$length];
		$chd.="|";
		
		for ($i = 1; $i < $length ; $i++) 
		$chd = $chd.$weekly_svn[$i].",";
			
		//Readjusting last argument
		$chd = $chd.$weekly_svn[$length]; 
		$chxl = $chxl."|"."Week".$i;
		
		echo '<br>This is chd argument:  '.$chd;
		echo '<br>This is chxl argument:  '.$chxl;
		
		$chxl.="|3:|No. of posts"; //Add number of posts label
		
		$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y".$chd.$chxl;
		echo "<br>".$graph."<br>";

		//Add colors to lines 
		$chco = "&chco=";
		
		for($i=0;$i<2;$i++) //Hard coded-- only 2 lines to plot
			$chco = $chco.$color[$i].",";
			$chco = substr($chco,0,-1); //handle last comma
		
		$graph.=$chco;
		
		//Add group labels
		$chdl ="&chdl=Forum|SVN";
		$graph.=$chdl;
		
		//Add line thickness
		
		$chls = "&chls=";
		$line_thickness=3;
		
		for($i=1;$i<=2;$i++) //can start from 0 since only color parameter is changed
			$chls = $chls.$line_thickness."|";
			$chls = substr($chls,0,-1); //handle last comma		
		
		$graph.=$chls;
		
		//Add background
		
		$backgrnd = "&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25";
		
		$graph.=$backgrnd;
		
		
		//Specify Y-Axis range
		//Adjust max post range to nearest multiple of 10
		while($max_post%10!=0)
		$max_post++;
		$max_post+=10; //plot 10 extra points for clarity
		
		$chxr="&chxr=1,0,".$max_post.",10"."|2,0,".$max_post.",10"."&chds=0,".$max_post; //$max_post - Find max number of posts of a group and specify range accordingly: 1-1st Y axis of (x,y,r,y) and 2-right Y-axis of (x,y,r,y)
		$graph.=$chxr;
		
		
		//Specify Y-axis label position
		$chxp="&chxp=3,50";
		
		$graph.=$chxp;
		
		//Chart title-- not implemented yet
		//$result_course =  mysql_query("SELECT title FROM `course`,`instructor` WHERE `instructor`.`full_name`=$_SESSION['logged_in'] AND group_id`=".$j." and date between ".$start_date." and ".strtotime('+7 days', $start_date)) or die(mysql_error());
		//$row_course = mysql_fetch_row($result_per_week_wiki);
		//$course = $row_course[0];
					
		$chtt = "&chtt=Correlation (Forum-SVN)&chts=000000,16";
		$graph.=$chtt;
		
		echo $graph;	
		
		//This are sample $graph actually generated
		#$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y&chd=t:4,4,4,18,75,21,5|5,4,4,19,30,11,1|10,10,8,30,30,12,4&chxl=0:|Week1|Week2|Week3|Week4|Week5|Week6|Week7|3:|No.%20of%20posts&chco=FF0000,00FF00,0000FF&chdl=etat|etat1|etat2&chls=3|3|3&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chxp=3,50";
		#$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y&chd=t:6,10,40,15,31|6,3,7,2,6|0,10,33,15,16&chxl=0:|Week1|Week2|Week3|Week4|Week5|3:|No.%20of%20posts&chco=FF0000,00FF00,0000FF&chdl=Group1|Group2|Group3&chls=3|3|3&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chxp=3,50";
		#$graph = "http://chart.apis.google.com/chart?chs=800x300&cht=lc&chxt=x,y,r,y&chd=t:6,10,40,15,31|6,3,7,2,6|0,10,33,15,16&chxl=0:|Week1|Week2|Week3|Week4|Week5|3:|No.%20of%20posts&chco=FF0000,00FF00,0000FF&chdl=Group1|Group2|Group3&chls=3|3|3&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chxr=1,0,50,10|2,0,50,10&chds=0,50&chxp=3,50&chtt=Weekly%20Chart%20(Course%201)&chts=000000,16";
		echo "<meta http-equiv=\"Refresh\" content=\"0; url=".$graph."\">";
		
		echo "<br><br><br><br><br>".$this->corr($weekly_forum,$weekly_svn);
		
		
		
	}
	
	function get_min_max_time()
	{
		
		// Get number of groups 
		$groups =  mysql_query("SELECT count(*) FROM `group`") or die(mysql_error());
		$row_groups = mysql_fetch_row($groups);
		#printf($row_groups[0]."<br>");
		
		/* Decide start and end date for the graph */
		
		//Get start date of forum posts
		for($j=1;$j<=$row_groups[0];$j++) //Plot graph for each group
		{
			$result_start_date =  mysql_query("SELECT MIN(date) FROM `forum` WHERE `group_id`=".$j) or die(mysql_error());
			$row_start_date = mysql_fetch_row($result_start_date);
			$min_date[$j-1]=$row_start_date[0];
			#printf($row_start_date[0]."<br>");
			
			//Get last date of forum posts
			$result_last_date =  mysql_query("SELECT MAX(date) FROM `forum` WHERE `group_id`=".$j) or die(mysql_error());
			$row_last_date = mysql_fetch_row($result_last_date);
			$max_date[$j-1]=$row_last_date[0];
			#printf($row_last_date[0]."<br>");
		}
		
		$time['min']=min($min_date);
		$time['max']=max($max_date);
		$time['num_groups']= $row_groups[0];
		
		return $time;	
	}
	
	/* Start of correlation logic */
	function corr($arr1, $arr2)
	{        
	    $correlation = 0;
	    
	    $k = $this->SumProductMeanDeviation($arr1, $arr2);
	    $ssmd1 = $this->SumSquareMeanDeviation($arr1);
	    $ssmd2 = $this->SumSquareMeanDeviation($arr2);
	    
	    $product = $ssmd1 * $ssmd2;
	    
	    $res = sqrt($product);
	    
	    $correlation = $k / $res;
	    
	    return $correlation;
	}
	
	function SumProductMeanDeviation($arr1, $arr2)
	{
	    $sum = 0;
	    
	    $num = count($arr1);
	    
	    for($i=0; $i<$num; $i++)
	    {
	        $sum = $sum + $this->ProductMeanDeviation($arr1, $arr2, $i);
	    }
	    
	    return $sum;
	}
	
	function ProductMeanDeviation($arr1, $arr2, $item)
	{
	    return ($this->MeanDeviation($arr1, $item) * $this->MeanDeviation($arr2, $item));
	}
	
	function SumSquareMeanDeviation($arr)
	{
	    $sum = 0;
	    
	    $num = count($arr);
	    
	    for($i=0; $i<$num; $i++)
	    {
	        $sum = $sum + $this->SquareMeanDeviation($arr, $i);
	    }
	    
	    return $sum;
	}
	
	function SquareMeanDeviation($arr, $item)
	{
	    return $this->MeanDeviation($arr, $item) * $this->MeanDeviation($arr, $item);
	}
	
	function SumMeanDeviation($arr)
	{
	    $sum = 0;
	    
	    $num = count($arr);
	    
	    for($i=0; $i<$num; $i++)
	    {
	        $sum = $sum + $this->MeanDeviation($arr, $i);
	    }
	    
	    return $sum;
	}
	
	function MeanDeviation($arr, $item)
	{
	    $average = $this->Average($arr);
	    
	    return $arr[$item] - $average;
	}    
	
	function Average($arr)
	{
	    $sum = $this->Sum($arr);
	    $num = count($arr);
	    
	    return $sum/$num;
	}
	
	function Sum($arr)
	{
	    return array_sum($arr);
	}
	
	/* End of correlation logic */

	
}

?>